package user;

import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import mysql.Conn;

@WebServlet(name = "signin", value = "/signin")
public class signin extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
		response.sendRedirect("http://localhost:8080/login.jsp");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException{
        response.setContentType("text/html");
        Conn conn = new Conn();
        PrintWriter out = response.getWriter();
        conn.getConn();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        try {
            ResultSet resultSet = conn.select(username);
            if (resultSet.next()) {
                boolean re = password.equals(resultSet.getString("password"));
                if( re == false) {
                    out.print("密码错误<a href = \"signin\">返回</a>");
                }else {
                    Cookie cook = new Cookie("username",resultSet.getString("username"));
                    response.addCookie(cook);
                    out.print("登陆成功<meta http-equiv=\"refresh\" content=\"1;url=home.jsp\">");

                }
            }else {
                out.print("用户不存在<a href = \"signin\">返回</a>");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
